{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a26d486b-c5ac-4b64-a7fb-5500c339dd3b",
   "metadata": {},
   "source": [
    "# Multiplication\n",
    "\n",
    "The multiplication operation, denoted '$*$', is a series of additions (\"long multiplication\").\n",
    "The multiplier has different implementations, depending on the type of adder in use.\n",
    "\n",
    "Note that integer and fixed-point numbers are represented in a two-complement method during function evaluation.\n",
    "The binary number is extended in the case of a register size mismatch.\n",
    "For example, the positive signed number $(110)_2=6$ is expressed as $(00110)_2$ when working with a five-qubit register.\n",
    "Similarly, the negative signed number $(110)_2=-2$ is expressed as $(11110)_2$.\n",
    "\n",
    "## Examples\n",
    "\n",
    "The calculation of -5 \\* 3 = -15.\n",
    "\n",
    "The left arg -5 is represented as 1011 and 3 as 11. The number of digits needed to store the answer is 4+2-1 = 5.\n",
    "The multiplication is done in the 'regular' manner where each number is extended to five bits and only five digits are kept in the intermediary\n",
    "results.\n",
    "\n",
    "$$\n",
    "\\begin{equation*}\\begin{array}{c}\n",
    "\\phantom{\\times}11011\\\\\n",
    "\\underline{\\times\\phantom{000}11}\\\\\n",
    "\\phantom{\\times}11011\\\\\n",
    "\\underline{\\phantom\\times1011\\phantom9}\\\\\n",
    "\\phantom\\times10001\n",
    "\\end{array}\\end{equation*}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c24bee78-f00a-4feb-80f6-a8a4f56c3506",
   "metadata": {},
   "source": [
    "## Examples\n",
    "\n",
    "#### Example 1: Two Quantum Variables Multiplication\n",
    "\n",
    "This code example generates a quantum program that multiplies two arguments.\n",
    "Both of them are defined as quantum variables of size 3."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "42c07535-40db-48ee-9c1e-7bd1d2995a5d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:22:47.409304Z",
     "iopub.status.busy": "2024-05-07T13:22:47.407608Z",
     "iopub.status.idle": "2024-05-07T13:22:50.053584Z",
     "shell.execute_reply": "2024-05-07T13:22:50.052743Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import Output, QArray, QBit, QNum, create_model, prepare_int, qfunc\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(a: Output[QNum], b: Output[QNum], res: Output[QNum]) -> None:\n",
    "    prepare_int(4, a)\n",
    "    prepare_int(5, b)\n",
    "    res |= a * b\n",
    "\n",
    "\n",
    "qmod = create_model(main)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4db98245-941f-42d7-bd79-b4f6b58d23dd",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:22:50.059265Z",
     "iopub.status.busy": "2024-05-07T13:22:50.057773Z",
     "iopub.status.idle": "2024-05-07T13:22:53.978877Z",
     "shell.execute_reply": "2024-05-07T13:22:53.978239Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'a': 4.0, 'b': 5.0, 'res': 20.0}: 1000]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from classiq import execute, synthesize, write_qmod\n",
    "\n",
    "write_qmod(qmod, \"multiplication_2vars_example\")\n",
    "qprog = synthesize(qmod)\n",
    "\n",
    "result = execute(qprog).result()[0].value\n",
    "result.parsed_counts"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56e1ff0a-822d-4512-ae1f-2aa6e2e0b285",
   "metadata": {},
   "source": [
    "#### Example 2: Float and Quantum Variable Multiplication\n",
    "\n",
    "This code example generates a quantum program that multiplies two arguments.\n",
    "Here, the left argument is a fixed-point number $(11.1)_2$ (3.5), \n",
    "and the right argument is a quantum variable of size 2."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6eb691d5-8e89-44e1-9c83-a9be9d771edb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:22:53.982620Z",
     "iopub.status.busy": "2024-05-07T13:22:53.982219Z",
     "iopub.status.idle": "2024-05-07T13:22:54.000365Z",
     "shell.execute_reply": "2024-05-07T13:22:53.999678Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import (\n",
    "    Output,\n",
    "    QArray,\n",
    "    QBit,\n",
    "    QNum,\n",
    "    allocate,\n",
    "    create_model,\n",
    "    hadamard_transform,\n",
    "    qfunc,\n",
    ")\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(a: Output[QNum], res: Output[QNum]) -> None:\n",
    "    allocate(2, a)\n",
    "\n",
    "    hadamard_transform(a)\n",
    "    res |= 3.5 * a\n",
    "\n",
    "\n",
    "qmod = create_model(main)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7c469852-14c7-43f8-8067-91820aa98ff7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:22:54.003932Z",
     "iopub.status.busy": "2024-05-07T13:22:54.003526Z",
     "iopub.status.idle": "2024-05-07T13:22:56.544546Z",
     "shell.execute_reply": "2024-05-07T13:22:56.543839Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'a': 2.0, 'res': 7.0}: 287,\n",
       " {'a': 3.0, 'res': 10.5}: 257,\n",
       " {'a': 1.0, 'res': 3.5}: 230,\n",
       " {'a': 0.0, 'res': 0.0}: 226]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from classiq import execute, synthesize, write_qmod\n",
    "\n",
    "write_qmod(qmod, \"multiplication_float_example\")\n",
    "qprog = synthesize(qmod)\n",
    "\n",
    "result = execute(qprog).result()[0].value\n",
    "result.parsed_counts"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
